<!-- 新增编辑车辆 -->
<template>
  <div>
    <el-dialog
      title="新增车辆"
      :visible.sync="showDialog"
      width="600px"
      @close="btnCancel"
    >
      <el-form
        ref="form"
        :rules="rules"
        :model="formData"
        label-width="110px"
        style="width: 100%"
      >
        <el-form-item
          label="车辆类型"
          prop="truckTypeId"
        >
          <el-select
            v-model="formData.truckTypeId"
            clearable
            placeholder="请选择"
          >
            <el-option
              v-for="item in settingList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="车牌号码"
          prop="licensePlate"
        >
          <el-input
            v-model="formData.licensePlate"
            placeholder="请输入车牌号码"
            maxlength="8"
          >
          </el-input>
        </el-form-item>
        <el-form-item
          label="GPS设备ID"
          prop="deviceGpsId"
        >
          <el-input
            v-model="formData.deviceGpsId"
            placeholder="请输入GPS设备ID"
            maxlength="20"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
        style="text-align: center"
      >
        <el-button
          class="save-btn"
          @click="btnOK"
        >确认</el-button>
        <el-button
          class="cancel-btn"
          @click="btnCancel"
        >取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { truckTypeAdd } from '@/api/transit'
export default {
  props: {
    currentNodeId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      showDialog: false,
      formData: {
        truckTypeId: '',
        licensePlate: '',
        deviceGpsId: ''
      },
      rules: {
        truckTypeId: [
          { required: true, message: '车辆类型不能为空', trigger: 'change' }
        ],
        licensePlate: [
          { required: true, message: '车牌号码不能为空', trigger: 'blur' },
          { validator: (rule, value, callback) => {
            if (!/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$/.test(value)) {
              callback(new Error('请输入正确格式的车牌号'))
            } else {
              callback()
            }
          }, trigger: 'blur' }
        ],
        deviceGpsId: [
          { required: true, message: 'GPS设备ID不能为空', trigger: 'blur' },
          { validator: (rule, value, callback) => {
            if (!/[A-Za-z0-9]+/.test(value)) {
              callback(new Error('请输入英文或数字'))
            } else {
              callback()
            }
          }, trigger: 'blur' }
        ]
      },
      settingList: []
    }
  },
  methods: {
    openDialog() {
      this.showDialog = true
    },
    btnCancel() {
      this.showDialog = false
      this.$refs.form.resetFields()
      this.formData = {
        truckTypeId: '',
        licensePlate: '',
        deviceGpsId: ''
      }
    },
    btnOK() {
      this.$refs.form.validate(async(isOK) => {
        if (isOK) {
          const res = await truckTypeAdd(this.formData)
          if (res.code === 200) {
            this.$message.success('新增车辆成功')
            this.$emit('updateVehicleList')
          } else {
            this.$message.error(res.msg)
          }
          this.btnCancel()
        }
      })
    },
    getSettingList(list) {
      this.settingList = list.slice(0)
    }
  }
}
</script>
<style scoped lang="scss">
::v-deep .el-dialog__body {
  padding: 40px 50px 0 20px !important;
}
</style>
